#pragma once
#include<assert.h>
namespace jxh {
	template<class T>
	struct list_node {
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

		list_node(const T& x = T())
			:_data(x)
			,_next(nullptr)
			,_prev(nullptr)
		{}


		template<class ...Args>
		list_node(Args&&...args)
			:_next(nullptr)
			,_prev(nullptr)
			,_data(forward<Args>(args)...)
		{}
		


	};

	template<class T,class Ref,class Ptr>
	struct __list_iterator {
		typedef list_node<T>Node;
		typedef __list_iterator<T, Ref, Ptr>self;
		Node* _node;
		__list_iterator(Node*node)
			:_node(node){}

		self& operator++() {
			_node = _node->_next;
			return *this;
		}
		self& operator--() {
			_node = _node->_prev;
			return *this;
		}
		self operator++(int) {
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self operator--(int) {
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		Ref operator*() {
			return _node->_data;
		}
		Ptr operator->() {
			return &_node->_data;
		}
		bool operator!=(const self& s)const {
			return _node != s._node;
		}
		bool operator==(const self& s)const {
			return _node == s._node;
		}
	};


	template<class T>
	class list {
		typedef list_node<T>Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T,const T&,const T*> const_iterator;


		iterator begin() {
			return _head->_next;
		}
		iterator end() {
			return _head;
		}
		const_iterator cbegin() {
			return const_iterator(_head->_next);
		}
		const_iterator cend() {
			return const_iterator(_head);
		}
		void empty_init() {
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list() {
			empty_init();
		}
		list(const list<T>& tmp) {
			empty_init();
			for (auto e : tmp) {
				push_back(e);
			}
		}
		

		void swap(list<T>& lt) {
			std::swap(_head, lt._head);
			std::swap(_size, lt._size());
		}
		list<int>& operator=(list<int>lt) {
			swap(lt);
			return *this;
		}
		~list() {
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear() {
			iterator it = begin();
			while (it != end()) {
				it = erase(it);
			}
		}
		void push_back(const T& x) {
			insert(end(), x);
		}
		template<class ...Args>
		void emplace_back(Args&&...args)
		{
			emplace(end(), forward<Args>(args)...);
		}





		void push_front(const T& x) {
			insert(begin(), x);
		}
		void pop_front() {
			erase(begin());
		}
		void pop_back() {
			erase(--end());
		}
		iterator insert(iterator pos, const T& x) {
			Node* cur = pos._node;
			Node* newnode = new Node(x);
			Node* prev = cur->_prev;
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			++_size;
			return newnode;
		}
		template<class ...Args>
		iterator emplace(iterator pos, Args&&... args)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(forward<Args>(args)...);
			Node* prev = cur->_prev;
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			++_size;
			return newnode;
		}
		iterator erase(iterator pos) {
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			delete cur;
			prev->_next = next;
			next->_prev = prev;
			--_size;
			return next;
		}
		size_t size() {
			return _size;
		}
	private:
		Node* _head;
		size_t _size;
	};

}
